package fatboy.hypeerweb;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * Implemented List structure
 * @author James
 *
 * @domain all valid NodePointer objects
 */
public class PointerList extends LinkedList<NodePointer>{
	public PointerList(){
		super();
	}
	public PointerList(PointerList p){
		super(p);
	}
	/**
	 * Adds node and stores based on value
	 * Stores either Surrogate Pointers OR Edge Pointers
	 * 
	 * @param toAdd object
	 * @return boolean based on success of add
	 */
	public boolean add(NodePointer toAdd){
		if(super.contains(toAdd)){
			return false;
		}
		
		//returns false if pointer types in list not same as one being added
		if(!super.isEmpty()&&!super.element().isSameType(toAdd)){
			
			return false;
		}
		
		int index = Collections.binarySearch(this, toAdd);
		
		super.add( -index - 1, toAdd);
		
		return true;
	}
	
	public String toString(){
		if(super.isEmpty()){
			return "Pointers: NONE";
		}
		else if(super.element() instanceof EdgePointer){
			return "Edge Pointers: "+super.toString();
		}
		return "Surrogate Pointers: "+super.toString();
		
	}
	
	public List<Node> getNodeList(){
		List<Node> toReturn = new LinkedList();
		for(int i = 0; i< size(); i++){
			if(get(i).getTypeOfNode().equals(NodeType.EDGE_NODE)){
				break;
			}
			toReturn.add(get(i).getNode());
		}
		return toReturn;
	}
}
